package org.lex.checksum.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import org.lex.checksum.core.AlgorithmRunner;

public class ProgressUpdater extends Timer implements ActionListener {
	private static final long serialVersionUID = 2885139103884470287L;
	private Checksum mainframe = null;
	private AlgorithmRunner cal = null;

	public ProgressUpdater(Checksum mainframe) {
		super(200, null);
		super.addActionListener(this);
		super.setInitialDelay(10);
		this.mainframe = mainframe;
	}

	public void monitor(AlgorithmRunner cal) {
		this.cal = cal;
		this.restart();
	}

	public void actionPerformed(ActionEvent e) {
		long progress = cal.getProgress();
		long max = cal.getLength();
		System.out.println(progress + "/" + max);
		int percent = Math.round(progress * 100f / max);
		System.out.println(percent);
		mainframe.setProgress(percent);
		if (percent >= 100) {
			this.stop();
		}
	}
}
